package sell;


import java.util.Date;

public class Main {
    private static Store store = new Store();
    private static int id = 0;

    public static void main(String[] args) {
        new Thread(new Runnable() {
            @Override
            public void run() {

                while (true){
                    synchronized (Main.class) {
                        id++;
                        store.put(id,new Goods(id,1.2,new Date()));
                        System.out.println("放入商品"  + store);
                    }

                }
            }
        },"工人一号").start();


        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    synchronized (Main.class) {
                        id++;
                        store.put(id,new Goods(id,1.2,new Date()));
                        System.out.println("放入商品"  + store);
                    }
                }
            }
        },"工人二号").start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true){
                    synchronized (Main.class) {
                        if (store.size() > 0) {
                            Object remove = store.remove(id);
                            System.out.println("拿走了" + id + "货物" );
                            id--;
                        }
                        else if (store.size() == 0) {
                            System.out.println("快点生产呀");
                            try {
                                Thread.sleep(1000);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }

                    }
                }
                }
            }
        },"消费者一号").start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true){
                    synchronized (Main.class) {
                        if (store.size() > 0) {
                            Object remove = store.remove(id);
                            System.out.println("拿走了" + id + "货物" );
                            id--;
                        }
                        else if (store.size() == 0) {
                            System.out.println("快点生产呀");
                            try {
                                Thread.sleep(1000);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }

                        }
                    }
                }
            }
        },"消费者二号").start();

    }


}

